package com.epfresh.api.utils;

import com.epfresh.api.utils.gson.TypeBuilder;
import com.google.gson.Gson;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;

/**
 * @author wangzhanghuan
 * @version 1.0
 * @date create 17-2-3
 * @Description
 */

public class JsonUtils {


    /*TypeBuilder

            一个用于生成泛型的简易Builder

    Example for List<String>

    Type type = TypeBuilder
            .newInstance(List.class)
            .addTypeParam(String.class)
            .build();
    Example for List<? super String>

    Type type = TypeBuilder
            .newInstance(List.class)
            .addTypeParamSuper(String.class)
            .build();
    Example for List<? extends CharSequence>

    Type type = TypeBuilder
            .newInstance(List.class)
            .addTypeParamExtends(CharSequence.class)
            .build();
    Example for Map<String, String[]>

    Type type = TypeBuilder
            .newInstance(HashMap.class)
            .addTypeParam(String.class)
            .addTypeParam(String[].class)
            .build();
    Example for Map<String, List<String>>

    Type type = TypeBuilder
            .newInstance(Map.class)
            .addTypeParam(String.class)
            .beginSubType(List.class) //开始 List<String> 部分
            .addTypeParam(String.class) //设置List的泛型值
            .endSubType() //结束 List<String> 部分
            .build();*/
    public static String toJson(Object o) {

        try {
            if (o != null)
                return new Gson().toJson(o);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
   
    public static <T> T fromJson(String s, Class<T> cls) {
        try {
            Gson gson = new Gson();
            T t = gson.fromJson(s, cls);
            return t;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    //RawType<List<T>>
    public static <T> T fromJsonArray(String s, Class rawType , Class clazz) {
        try {
            Type t = TypeBuilder
                    .newInstance(rawType)
                    .beginSubType(List.class)
                    .addTypeParam(clazz)
                    .endSubType()
                    .build();
            return new Gson().fromJson(s, t);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    //RawType<T>
    public static <T> T fromJsonObject(String s, Class rawType , Class clazz) {
        try {
            Type t = TypeBuilder
                    .newInstance(rawType)
                    .addTypeParam(clazz)
                    .build();
            return new Gson().fromJson(s, t);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    //List<T>
    public static <T> T fromJsonList(String s, Class clazz) {
        return fromJsonObject(s, List.class, clazz);
    }

    //RawType<Map<String,T>>
    public static <T> T fromJsonRawTypeMap(String s, Class rawType , Class clazz) {
        try {
            Type t = TypeBuilder
                    .newInstance(rawType)
                    .beginSubType(HashMap.class)
                    .addTypeParam(String.class)
                    .addTypeParam(clazz)
                    .endSubType()
                    .build();
            return new Gson().fromJson(s, t);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
    //RawType<Map<String,T>>
    public static <T> T fromJsonRawTypeMap(String s, Class clazz) {
        try {
            Type t = TypeBuilder
                    .newInstance(HashMap.class)

                    .addTypeParam(String.class)
                    .addTypeParam(clazz)
                    .endSubType()
                    .build();
            return new Gson().fromJson(s, t);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
    //Map<String,T>
    public static <T> T fromJsonMap(String s, Class clazz) {
        try {
            Type t = TypeBuilder
                    .newInstance(HashMap.class)
                    .addTypeParam(String.class)
                    .addTypeParam(clazz)
                    .build();
            return new Gson().fromJson(s, t);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }


}
